import { createEntityAdapter, createSlice, Draft, EntityState, PayloadAction } from "@reduxjs/toolkit";
import { AppState } from "@store/index";
const commentsEntityAdaptor = createEntityAdapter<any>({
  //@ts-ignore
    selectId: (state:any) => state.com_id,
  });
  const { reducer: commentsReducer, actions } = createSlice<
  EntityState<ArticleComment,number>,
  {
    // 保存评论列表
    saveComments(
      state: Draft<EntityState<ArticleComment,number>>,
      action: PayloadAction<ArticleComment[]>
    ): void;
    // 清除评论列表
    clearComments(state: Draft<EntityState<ArticleComment,number>>): void;
  },
  "comments",
  never
>({
  name: "comments",
  
  initialState: commentsEntityAdaptor.getInitialState(),
  reducers: {
    // 保存评论列表
    saveComments: commentsEntityAdaptor.addMany,
    // 清除评论列表
    clearComments: commentsEntityAdaptor.removeAll,
  },
} as any);
export const commentSelectors = commentsEntityAdaptor.getSelectors<AppState>(
    (state:any) => state.commentsReducer
  );
  // 导出和评论相关的 action creator 函数
  export const { saveComments, clearComments } = actions;
  
  // 导出用于管理评论列表的 reducer 函数
  export default commentsReducer;